<template>
  <div class="container">
    <el-table :data="tableDataFn">
      <el-table-column type="index" width="50" label="序号"> </el-table-column>
      <template v-for="item in tableColumns">
        <el-table-column
          :prop="item.prop"
          :label="item.label"
          :width="item.width"
          align="center"
          :key="item.prop"
        >
          <template v-slot="scope">
            <template v-if="item.format == 'ALERT_LEVEL'">
              <div style="text-align: center">
                <el-tag
                  :type="scope.row.levelType"
                  disable-transitions
                  size="mini"
                  effect="dark"
                >
                  {{ scope.row[item.prop] }}
                </el-tag>
              </div>
            </template>
            <template v-else>
              <div>
                {{ scope.row[item.prop] }}
              </div>
            </template>
          </template>
        </el-table-column>
      </template>
    </el-table>
  </div>
</template>>

<script>
import bus from "common/bus.js";
import { fetchData } from "@/api/index.js";
import tableColumns from "./WarningLog.js";
import moment from "moment";
export default {
  data() {
    return {
      postData: {
        opera: "dbalertlist",
        curPage: 1,
        pageSize: 20,
        sort: {
          sortColumn: "",
          sortOrders: "",
        },
        filtersType: "fuzzyquery",
        filters: {
          monid: "",
        },
      },
      tableData: [],
      tableColumns: tableColumns,
      selectedMonID: {},
    };
  },
  components: {},
  created() {
    this.selectedMonID = this.$store.getters["ItemMonitorALL/getItem"];
    // console.log(this.selectedMonID)
    this.postData.filters.monid = this.selectedMonID.MONID;
    this.initGetData();
  },
  mounted() {
    bus.$on("ItemMonitorALL", (msg) => {
      this.selectedMonID = msg;
      this.postData.filters.monid = this.selectedMonID.MONID;

      this.initGetData();
    });
  },
  computed: {
    tableDataFn() {
      return this.tableData.filter((item) => {
        switch (item.ALERT_LEVEL) {
          case 1:
            item.ALERT_LEVEL = "提醒";
            item.levelType = "info";
            break;
          case 2:
            item.ALERT_LEVEL = "普通";
            item.levelType = "warning";
            break;
          case 3:
            item.ALERT_LEVEL = "紧急";
            item.levelType = "danger";
            break;

          default:
            break;
        }

        return item;
      });
    },
  },
  methods: {
    initGetData() {
      fetchData({
        postData: JSON.stringify(this.postData),
      }).then((res) => {
        if (res.data.length > 0 && res.data[0].RECORD_TIME == null) {
          res.data[0].RECORD_TIME = moment(new Date()).format(
            "YYYY-MM-DD HH:mm:ss"
          );
        }
        this.tableData = res.data;
      });
    },
  },
  beforeDestroy() {
    bus.$emit("timerNewTodo", {
      name: this.timerName,
    });
  },
};
</script>

<style scoped>
</style>


